package com.leyou.upload.controller;

import com.leyou.upload.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author CHERRY
 * @date 2018-09-03-0:19
 */
@Controller
public class UploadController {
    @Autowired
    private UploadService uploadService;

    @PostMapping("upload/image")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        String url = this.uploadService.uploadFile(file);
        if (StringUtils.isEmpty(url)) {
            return ResponseEntity.badRequest().build();
        }
        //上传成功，回显数据
        return ResponseEntity.status(HttpStatus.CREATED).body(url);
    }

}
